﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;

namespace Npu_Platform
{
    public partial class ClientForm : Form
    {
        Client client;
        delegate void SetTextCallback(String Text);
        delegate void MessageBoxCallback(Exception Ex);
        public ClientForm()
        {
            InitializeComponent();
        }

        private void ClientForm_Load(object sender, EventArgs e)
        {
            client = new Client(IPAddress.Parse(Ipav6.Text), int.Parse(Port.Text));
            client.SendMessage += new Client.SendMessageEventHandler(DisplayMessage);
            client.SendError += new Client.SendErrorEventHandler(DisplayError);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(Send));
        }
        private void Send(Object stateInfo)
        {

            client.ClientInit();

        }
        public void ShowError(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        public void DisplayError(Exception ex)
        {
            ClientForm.MessageBoxCallback d = new ClientForm.MessageBoxCallback(ShowError);
        }
        public void DisplayMessage(String s)
        {
            ClientForm.SetTextCallback d = new ClientForm.SetTextCallback(ShowMessage);
            this.Invoke(d, new object[] { s });
        }
        public void ShowMessage(String s)
        {
            MessageText.Text += s + "\r\n";
        }
    }
}
